layui.use(['form','layer','laydate','table','util'],function (){
    let form = layui.form,layer = layui.layer,laydate = layui.laydate,table = layui.table,util = layui.util;
    let dateElem = $('.form-dateElem');
    //渲染日期模块，最大日期为当前日期
    layui.each(dateElem,function () {
        laydate.render({
            elem:this,
            //最大日期比今天多几天
            max:0,
        })
    })

    //监听table，修改受理状态：档案数字化、数字化完成
    table.on('toolbar(fistDataTable)',function (obj) {
        //获取监听事件
        let layEvent = obj.event;

        //获取所有的选中的数据
        let aceptData = layui.table.checkStatus("dataTableId").data;
        if (aceptData.length <= 0){
            layer.msg("请选择一条数据",{icon:3,time:1500});
            return;
        }
        //获取业务申请编号集合
        let atBussIdList = getAtBussIdList(aceptData);
        let jsonData = {
            "atBussIdList" : atBussIdList,
            "pdTpCd" : ""
        }
        if (valueIsEqual(layEvent,"accepted")){
            for (let i=0;i<aceptData.length;i++){
                let std = aceptData[i].atAcptStdNm;
                let compNm = aceptData[i].atCompNm;
                if (valueIsEqual(std,"档案入库")){
                    top.layer.msg("企业名称："+compNm+" 的企业已经入库！",{icon:3,time:2000});
                    return;
                }
            }
            jsonData.pdTpCd = "03"
        }
        sendAjax("/circulation/archivesDigitizing/updateAceptInf",jsonData,function (res) {
            if (valueIsEqual(res.code,"00")){
                top.layer.msg("修改受理状态成功！",{icon:6,time:1500});
            }else {
                top.layer.msg("修改受理状态失败！",{icon:6,time:1500});
            }
            queryArchivesAcceptInfo();
        })
    });
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/22
* @description: 初始化生成table数据
*/
$(function () {
    queryArchivesAcceptInfo();
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/22
* @description: 生成table数据
*/
function queryArchivesAcceptInfo() {
    //起始日期
    let stDt = $('#stDt').val();
    //截止日期
    let lmDt = $('#lmDt').val();
    //查询条件类型
    let srchReqFir = $("select[name = 'srchReqFir']").val();
    //查询条件
    let qryReqFir = $("input[name = 'qryReqFir']").val().trim();

    let stDtClean = valueClean(stDt,"-");
    let lmDtClean = valueClean(lmDt,"-");
    if (!valueIsEmpty(stDt) && ! valueIsEmpty(lmDt) && stDtClean > lmDtClean){
        top.layer.alert("起始日期不能大于截止日期");
        return;
    }
    let jsonData = {
        "srchReqFir" : srchReqFir,
        "qryReqFir" : qryReqFir,
        //传不为空的任意值
        "qryReqSeco": "KD03",
        "stDt" : stDt,
        "lmDt" : lmDt
    }
    //时间戳
    let tableTitle = '企业受理信息表'+layui.util.toDateString(new Date(),'yyyyMMddHHmmss');
    layui.table.render({
        elem:'#dataTable',
        id:'dataTableId',
        url:'/business/acceptAdmin/selectAcceptInfo',
        parseData:function (result) {
            return{
                "code":result.code,
                "msg":result.msg,
                "count":result.data.pageCount,
                "data":result.data.list
            }
        },
        size:'sm',
        //分页的起止页号，每页条数
        page:{
            curr:1,
            limit:15,
            limits:[15,30,50,100]
        },
        //自定义的页码参数名称、每页条数参数名称
        request:{
            pageName:'pageNum',
            limitName:'pageSize'
        },
        //table的高度
        height:550,
        //查询条件，json格式
        where:jsonData,
        cols:[[
            {type:'checkbox'},
            {type:'numbers',title:'序号'},
            {field:'atBundleNo',title:'捆号'},
            {field:'atAcptId',title:'受理号'},
            {field:'atCompNm',title:'企业名称',width:300},
            {field:'atAcpterNm',title:'受理人',width:100},
            {field:'atAcptDt',title:'受理日期',width: 100,templet:function (d) {
                    return layui.util.toDateString(d.atAcptDt,"yyyy-MM-dd");
                }},
            {field:'atRegistId',title:'注册号',width: 150},
            {field:'atCreditCode',title:'统一社会信用代码'},
            {field:'atArchType',title:'档案类型',width:100},
            {field:'atAcptStdNm',title:'受理状态',width: 100},
            {field:'atAresTolNum',title:'档案页数',width: 100,templet:function (d){
                return d.atAresTolNum+'页';
                }},
            {field:'atBussId',title:'业务申请编号',hide:true}
        ]],
        //头部工具条
        toolbar: '#totalToolBar',
        //导出时候excel文件标题
        title:tableTitle
    });
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/12/2
* @description: 获取选中行的业务申请编号集合
*/
function getAtBussIdList(aceptData) {
    let atBussIdList = "";
    for (let i=0;i < aceptData.length; i++){
        if (valueIsEqual(atBussIdList,"")){
            atBussIdList = aceptData[i].atBussId ;
        }else {
            atBussIdList = atBussIdList + "," + aceptData[i].atBussId;
        }
    }
    return atBussIdList;
}